﻿module Sustainalytics.ValidationRules.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;
    import LRTM = Sustainalytics.LRTM;
    import Routing = Sustainalytics.Routing;

    export class ValidationRulesController {

        // controller scope
        public $scope: ng.IScope;
        // services
        private $q: ng.IQService;
        private $location: ng.ILocationService;
        private $routeParams: any;
        private notificationFactory: Common.INotificationFactory;
        private staticObjectsAdmin: CommonAdmin.IStaticObjects;

        // specifies whether current controller is in loading state
        public isLoading: boolean;
        public uploadProgress: number;

        //private inputFile: PIInputFileDto;
        private selectedFile: File;
        private backendService: ValidationRulesBackendService;

        private warningMessages: string[];

        constructor(
            $scope: ng.IScope,
            $q: ng.IQService,
            $location: ng.ILocationService,
            private RoutingService: Routing.IRoutingService,
            backend: ValidationRulesBackendService,
            private LongRunningTaskManagerService: LRTM.LongRunningTaskManagerService,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects) {

            var ctx = this;

            ctx.$scope = $scope;
            ctx.$q = $q;
            ctx.$location = $location;            
            ctx.notificationFactory = notificationFactory;
            ctx.staticObjectsAdmin = staticObjectsAdmin;
            ctx.$routeParams = RoutingService.getRouteParams();
            ctx.backendService = backend;

            ctx.uploadProgress = -1;
         
            // scope
            $scope['validationRules'] = ctx;
            // notify parents that the controller was created
            $scope.$emit('validationRules.created', ctx);
        }

        // excel selection
        public onSelectFile(files): any {
            var ctx = this;

            ctx.selectedFile = null;
            var file = files[0]; 
            console.log(file.type);
            if (file && (file.type != "application/csv" && file.type !="application/vnd.ms-excel")) {
                ctx.notificationFactory.warn('Incorrect file type, Csv file type is required.');
            }
            else if (file) {
                ctx.selectedFile = file;
            }
        }

        public downloadWhiteListFile(): any {           
            var ctx = this;
            var promise = ctx.LongRunningTaskManagerService.runTask("whiteList.csv", null, LRTM.LongRunningTaskTypeEnum.ValidationRulesWhiteListCSV, true, null);        
        }

        public uploadInputFile(): any {
            var ctx = this;
            if (ctx.selectedFile) {
                ctx.uploadProgress = 0;
                var promise = ctx.backendService.uploadWhiteListFile(ctx.selectedFile, ctx.uploadProgress);
                    promise
                        .then(
                        // success 
                        (data: { warningMessages: any;}) => {
                            // handles  validation /operation result
                            ctx.$scope.$evalAsync(() => {
                                console.log(data.warningMessages);

                                var warnings = JSON.parse(data.warningMessages);

                                if (warnings.length > 0) {
                                    
                                    ctx.warningMessages = warnings;
                                    ctx.notificationFactory.success('File processed with warnings.');
                                }
                                else {
                                    ctx.warningMessages = null;
                                    ctx.notificationFactory.success('File processed successfully.');
                                }
                                
                                ctx.uploadProgress = -1;
                            });
                        },
                        //error
                        (data: { code: number  }) => {
                            ctx.notificationFactory.error('Upload failed','Failed to process file.');
                            ctx.uploadProgress = -1;
                        });
            }
        }
    }


    // register controller with News Angular module
    var mod = angular.module(validationRulesAdminModuleName);

    mod.controller('ValidationRulesController',
        ['$scope', '$q', '$location', 'RoutingService', 'ValidationRulesBackendService', 'LongRunningTaskManagerService', 'notificationFactory', 'staticObjectsAdmin', ValidationRulesController]);
}